package Tools;

import java.io.File;
import java.io.FilenameFilter;
import java.sql.Date;

/**
This class implements base functions of file manager
@author Ivan
*/
public class TBrowser {

	private File root;
	
	public TBrowser(){
		root = null;
	}
	
	/**
	 * This method displays the content of the directory which specified as parameter.  
	 */
	public void ShowFiles(String directory) {
		try{
		   root = new File(directory);
		
		   for (File child : root.listFiles()) 
			   System.out.print(child.getName() +"\n");
		}
		catch (NullPointerException e) {
			System.out.print("Error! Directory is not exist! "+ e.getMessage());
		}
		catch (SecurityException e) {
			System.out.print("Error of read! Access denied to the directory! " +  e.getMessage());
		}

	}
	
	/**
	 * This method  displays the directory tree and theirs content in console window. 
	 * Name of initial directory transfered as input parameter.
	 */
	public void ShowTreeOfDirectories(String directory) {
		try{
		    root = new File(directory);
		
		    for (File child : root.listFiles()){ 
			    System.out.print(child.getAbsolutePath() +"\n");
	
			    if (child.list() != null)
				    ShowTreeOfDirectories(child.getAbsolutePath());				
		    }	
		}
		catch (NullPointerException e) {
			System.out.print("Error! Directory is not exist! "+ e.getMessage());
		}
		catch (SecurityException e) {
			System.out.print("Error of read! Access denied to the directory! " + e.getMessage());
		}
	}
	
	/**
	 *This method displays the content of the directory with extended information using searching mask.
	 *Directory and mask of search specified as parameters this methods.
	 */
	public void ShowFiles(String directory, FilenameFilter MaskOfSearch)
	{	
		try{
		    root = new File(directory);
		    
		    //array of files which satisfy requirement mask of searching
		    File[] processed_files = root.listFiles(MaskOfSearch);
		    
		    System.out.print("(Amount of found files : " + processed_files.length + ") \n ");
		    for (File file : processed_files )
		    	System.out.print("Name of file : " + file.getName() 
   			         + " ; full path : " + file.getAbsolutePath()
   			         +  " ; date of last change : " + new Date(file.lastModified()).toString() +" ; \n");
		  
		    	
		}
		catch (NullPointerException e) {
			System.out.print("Error! Directory is not exist! "+ e.getMessage());
		}
		catch (SecurityException e) {
			System.out.print("Error of read! Access denied to the directory! " + e.getMessage());
		}
		
    }
	
}

